package turing.forum.repository.po;


import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.apache.catalina.User;
import turing.forum.web.vo.UserShareVo;

import java.util.Date;

@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
@TableName("user_shares")
public class UserShare {
    @TableId(type = IdType.ASSIGN_ID)
    private Long id;
    @TableField(value = "user_id")
    private Long userId;
    @TableField(exist = false)
    private String content;
    @TableField(exist = false)
    private String pictures;
    @Version
    private Integer version;
    @TableLogic
    private Integer deleted;
    @JsonFormat(pattern = "yyyy-MM-dd")
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private Date createTime;
    @JsonFormat(pattern = "yyyy-MM-dd")
    @TableField(value = "create_time", fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    public UserShare(UserShareVo shareVo) {
        this.userId = shareVo.getUserId();
        this.content = shareVo.getContent();
        String[] pictures = shareVo.getPictures();
        if (pictures == null || pictures.length == 0) {
            this.pictures = "";
        } else if (pictures.length > 0) {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < pictures.length; i++) {
                builder.append(pictures[i]).append("\n");
            }
            builder.setLength(builder.length() - 1);
            this.pictures = builder.toString();
        }
    }
}
